import Vue from 'vue'

export default{
    namespaced:true, //开启命名空间
    state:()=>({
        cartList:[
                // {
                //     "price_id": "20510",
                //     "goods_id": "7937",
                //     "goods_no": "F07937",
                //     "goods_name": "小幸运-33枝粉玫瑰花束",
                //     "out_sku": null,
                //     "spec_ids": "",
                //     "spec_name": "",
                //     "stock": "999",
                //     "sell_price": "258.00",
                //     "market_price": "288.00",
                //     "cost_price": null,
                //     "weight": null,
                //     "cover": "http://res.xiangsidi.com/goodsphoto/vws6LfpyMQT7fxRKtIptUyBaJJwpVVPee1jVYfXk.jpeg@0o_0l_300w_100q.src",
                //     "ware_dec": "精选33枝粉佳人（可用戴安娜）玫瑰+银叶菊+满天星点缀，内衬浅蓝色雾面纸圆形包装，外部白色雾面包装纸扇形包装，",
                //     "sale_count": "12",
                //     "review_count": "0",
                //     "browse_count": "249",
                //     "is_sale": "1",
                //     "is_show_list": "1",
                //     "subtitle": "原来你是我最想留住的幸运， 原来我们和爱情曾经靠得那么近",
                //     "favorite": null,
                //     "browse": "26268",
                //     "def_cover": "goodsphoto/vws6LfpyMQT7fxRKtIptUyBaJJwpVVPee1jVYfXk.jpeg",
                //     "type_id": "1",
                //     "num":1
                // },
                // {
                //     "price_id": "20509",
                //     "goods_id": "8220",
                //     "goods_no": "F08220",
                //     "goods_name": "百花争艳-33朵红色粉色玫瑰混搭",
                //     "out_sku": null,
                //     "spec_ids": "",
                //     "spec_name": "",
                //     "stock": "1000",
                //     "sell_price": "288.00",
                //     "market_price": "458.00",
                //     "cost_price": null,
                //     "weight": null,
                //     "cover": "http://res.xiangsidi.com///attachment/goods/350/201812/5c28357020aaf.jpg@0o_0l_300w_100q.src",
                //     "ware_dec": "24朵粉红雪山玫瑰，9朵红玫瑰,3个乒乓菊，红豆、粉色满天星间插丰满。，（不少于三张）牛油纸、（不少于3张）雪梨纸内衬,（不少于8张）粉色雾面纸外包装，白色、灰色英文丝带蝴蝶结束扎。，",
                //     "sale_count": "14",
                //     "review_count": "0",
                //     "browse_count": "1277",
                //     "is_sale": "1",
                //     "is_show_list": "1",
                //     "subtitle": "爱之初体验，一心一意只为你",
                //     "favorite": null,
                //     "browse": "53397",
                //     "def_cover": "/attachment/goods/800/201812/5c28357020aaf.jpg",
                //     "type_id": "1",
                //     "num":1
                // },
            ],
        xqlist:null,
    }),
    getters: {
        allInfo(state){
            let allNum=0
            let allPrice=0
            state.cartList.forEach(item=>{
                allNum += item.num
                allPrice += item.num * item.sell_price
            })
            return {
                allPrice,
                allNum
            }
        }
    },
    mutations:{
        deAddMut(state,obj){
            state.xqlist=obj
        },
        detAddMut(state,obj){
            state.xqlist=obj
        },
        detaAddMut(state,obj){
            state.xqlist=obj
        },
        detaiAddMut(state,obj){
            state.xqlist=obj
        },
        detailAddMut(state,obj){
            state.xqlist=obj
        },
        cartNumMut(state,{boll=true,idx}){  //boll为了区分加减，true为加
            let {num} = state.cartList[idx]
            if(boll){
                if(num<6){
                    state.cartList[idx].num++
                }
            }else{
                if(num>1){
                    state.cartList[idx].num--
                }
            }   
        },
        cartDelMut(state,idx){ //删除
            state.cartList.splice(idx,1)
        },
        cartAllDel(state){  //清空购物车所有商品
            for(let i=state.cartList.length-1;i>=0;i--){
                state.cartList.splice(i,1)
            }
        },
        cartAddMut(state,obj){  //加入购物车
            let {cartList} = state
            let len = cartList.length
            for(let i=0;i<len;i++){
                if(cartList[i].browse==obj.browse){  //判断是否有同类商品
                    state.cartList[i].num++
                    return
                }
            }
            Vue.set(obj,'num',1) //给新商品追加字段
            state.cartList.push(obj)
        }
    }
}